import { conversations } from '@/api/ai'
import { getchattopic } from '@/api/chat'
import qs from "qs";

const chat = {
  state: {
    chatfromdify: true,
    chatList: [],
    currentTopicId: ""
  },

  mutations: {
    SET_CHATLIST: (state, chatList) => {
      state.chatList = chatList
    },
    SET_CURRENTTOPICID: (state, topicId) => {

      state.currentTopicId = topicId
    }
  },

  actions: {
    // 登录
    async getchattopic({ state,
      commit,
      dispatch,
      rootState, }) {
      let data = {
        user: rootState.user.name,
        limit: 100,
      };
      let res
      if (state.chatfromdify) {
        res = await conversations(data);
      } else {
        res = await getchattopic()
      }
    
      let showlist =[]
      if(res.data && res.data.length ) {
         showlist = res.data.slice(0,10)
       
      }
 
      commit('SET_CHATLIST', showlist)


    },
    setCurrentTopicId({ commit }, data) {
 
      commit('SET_CURRENTTOPICID', data)
    },

  },
  namespaced: true,
}

export default chat
